{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A Cantera Simulation Using RMG-Py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display, Image\n",
    "\n",
    "from rmgpy.chemkin import load_chemkin_file\n",
    "from rmgpy.tools.canteraModel import Cantera, get_rmg_species_from_user_species\n",
    "from rmgpy.species import Species"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load the species and reaction from the RMG-generated chemkin file `chem_annotated.inp` and `species_dictionary.txt` file found in your `chemkin` folder after running a job."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "species_list, reaction_list = load_chemkin_file('data/ethane_model/chem_annotated.inp',\n",
    "                                                'data/ethane_model/species_dictionary.txt',\n",
    "                                                'data/ethane_model/tran.dat')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set a few conditions for how to react the system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Find the species: ethane\n",
    "user_ethane=Species().from_smiles('CC')\n",
    "species_dict = get_rmg_species_from_user_species([user_ethane], species_list)\n",
    "ethane = species_dict[user_ethane]\n",
    "\n",
    "reactor_type_list = ['IdealGasReactor']\n",
    "mol_frac_list=[{ethane: 1}]\n",
    "Tlist = ([1300, 1500, 2000], 'K')\n",
    "Plist = ([1], 'bar')\n",
    "reaction_time_list = ([0.5], 'ms')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create cantera object, loading in the species and reactions\n",
    "job = Cantera(species_list=species_list, reaction_list=reaction_list, output_directory='temp')\n",
    "# The cantera file must be created from an associated chemkin file\n",
    "\n",
    "# We can either load the Model from the initialized set of rmg species and reactions\n",
    "job.load_model()\n",
    "\n",
    "# Or load it from a chemkin file by uncommenting the following line:\n",
    "#job.load_chemkin_model('data/ethane_model/chem_annotated.inp',transport_file='data/ethane_model/tran.dat')\n",
    "\n",
    "# Generate the conditions based on the settings we declared earlier\n",
    "job.generate_conditions(reactor_type_list, reaction_time_list, mol_frac_list, Tlist, Plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Simulate and plot\n",
    "alldata = job.simulate()\n",
    "job.plot(alldata)\n",
    "\n",
    "# Show the plots in the ipython notebook\n",
    "for i, condition in enumerate(job.conditions):\n",
    "    print('Condition {0}'.format(i+1))\n",
    "    display(Image(filename=\"temp/{0}_mole_fractions.png\".format(i+1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We can get the cantera model Solution's species and reactions\n",
    "ct_species = job.model.species()\n",
    "ct_reactions = job.model.reactions()\n",
    "\n",
    "# We can view a cantera species or reaction object from this\n",
    "ct_ethane = ct_species[4]\n",
    "ct_rxn = ct_reactions[0]\n",
    "\n",
    "print(ct_ethane)\n",
    "print(ct_rxn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We can also do things like modifying the cantera species thermo and reaction kinetics through modifying the\n",
    "# RMG objects first, then using the `modifyReactionKinetics` or `modifySpeciesThermo` functions\n",
    "\n",
    "# Alter the RMG objects in place, lets pick ethane and the first reaction\n",
    "rmg_ethane = species_dict[user_ethane]\n",
    "rmg_ethane.thermo.change_base_enthalpy(2*4184)  # Change base enthalpy by 2 kcal/mol\n",
    "\n",
    "rmg_rxn = reaction_list[0]\n",
    "rmg_rxn.kinetics.change_rate(4)  # Change A factor by multiplying by a factor of 4\n",
    "\n",
    "# Take a look at the state of the cantera model before and after\n",
    "print('Cantera Model: Before')\n",
    "ct_species = job.model.species()\n",
    "ct_reactions = job.model.reactions()\n",
    "print('Ethane Thermo = {} kcal/mol'.format(ct_species[4].thermo.h(300)/1000/4184))\n",
    "print('Reaction 1 Kinetics = {}'.format(ct_reactions[0].rate))\n",
    "\n",
    "# Now use the altered RMG objects to modify the kinetics and thermo\n",
    "job.modify_reaction_kinetics(0, rmg_rxn)\n",
    "job.modify_species_thermo(4, rmg_ethane, use_chemkin_identifier = True)\n",
    "\n",
    "# If we modify thermo, the cantera model must be refreshed.  If only kinetics are modified, this does not need to be done.\n",
    "job.refresh_model()\n",
    "\n",
    "print('')\n",
    "print('Cantera Model: After')\n",
    "ct_species = job.model.species()\n",
    "ct_reactions = job.model.reactions()\n",
    "print('Ethane Thermo = {} kcal/mol'.format(ct_species[4].thermo.h(300)/1000/4184))\n",
    "print('Reaction 1 Kinetics = {}'.format(ct_reactions[0].rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Simulate and plot\n",
    "alldata = job.simulate()\n",
    "job.plot(alldata)\n",
    "\n",
    "# Show the plots in the ipython notebook\n",
    "for i, condition in enumerate(job.conditions):\n",
    "    print('Condition {0}'.format(i+1))\n",
    "    display(Image(filename=\"temp/{0}_mole_fractions.png\".format(i+1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:rmg_env]",
   "language": "python",
   "name": "conda-env-rmg_env-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
